package com.weiwei.diancan.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.weiwei.diancan.Contant.ConfigMethod;
import com.weiwei.diancan.R;
import com.weiwei.diancan.adapter.CaozuoAdapter;
import com.weiwei.diancan.adapter.OrderDetailAndAdapter;
import com.weiwei.diancan.base.BaseActivity;
import com.weiwei.diancan.bean.JiluBean;
import com.weiwei.diancan.bean.LoginBean;
import com.weiwei.diancan.cache.CacheConfig;
import com.weiwei.diancan.divider.RecycleViewDivider;
import com.weiwei.diancan.http.RetrofitFactory;
import com.weiwei.diancan.utils.HttpUtils;
import com.weiwei.diancan.utils.LoggerUtils;
import com.weiwei.diancan.view.EmptyLayout;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import weiwei.com.weiweilibrary.views.ToastUtil;

public class OrderDetailAndActivity extends BaseActivity {

    private RecyclerView recycler;
    private TextView tv_jilu;
    private boolean isShow = false;//记录操作记录是否展开的开关
    private LoginBean.OrderListBean orderListBean;
    private List<LoginBean.OrderListBean.LogListBean> log_list;
    private OrderDetailAndAdapter adapter;
    private TextView tv_price;
    private TextView tv_no;
    private String orderNo,time;
    private String orderId;
    private RecyclerView rv_caozuojilu;

    @Override
    public void getData() {
        double price = 0;
        //List<LoginBean.OrderListBean> order_list = tabListBean.getOrder_list();
                List<LoginBean.OrderListBean.LogListBean> log_list = orderListBean.getLog_list();
                //if(orderListBean.getOrder_type().equals("0")) {
                    orderNo = orderListBean.getOrder_no();
                    orderId = orderListBean.getOrder_id();
                    time = orderListBean.getInsert_time();
                    if (log_list != null) {
                        for (LoginBean.OrderListBean.LogListBean logListBean : log_list) {
                            double log_money = Double.parseDouble(logListBean.getLog_money());
                            price+=log_money;
                        }
                    }
        //}
        String format = new DecimalFormat("#.00").format(price);
        SpannableStringBuilder sb = new SpannableStringBuilder("小计：¥"+format);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#B41B1B"));
        sb.setSpan(foregroundColorSpan,3,sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_price.setText(sb);

        if(orderListBean.getOrder_type().equals("1")) {
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder("追加订单：" + orderNo + "(" + time + ")");
            stringBuilder.setSpan(foregroundColorSpan,0,5+orderNo.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv_no.setText(stringBuilder);
        }else{
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder("主订单：" + orderNo + "(" + time + ")");
            stringBuilder.setSpan(foregroundColorSpan,0,4+orderNo.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv_no.setText(stringBuilder);
        }

        orderJilu();
    }

    @Override
    public void setListeners() {
        tv_jilu.setOnClickListener(v -> {
            if(isShow){
                tv_jilu.setTextColor(Color.parseColor("#181818"));
                Drawable drawable= getResources().getDrawable(R.mipmap.jiantou_js);
                /// 这一步必须要做,否则不会显示.
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                tv_jilu.setCompoundDrawables(null,null,drawable,null);
                rv_caozuojilu.setVisibility(View.GONE);
                isShow = false;
            }else{
                tv_jilu.setTextColor(Color.parseColor("#777777"));
                Drawable drawable= getResources().getDrawable(R.mipmap.icon_xialajiantou_spxq);
                /// 这一步必须要做,否则不会显示.
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                tv_jilu.setCompoundDrawables(null,null,drawable,null);
                rv_caozuojilu.setVisibility(View.VISIBLE);
                isShow = true;
            }
        });
    }

    @Override
    public void initView() {
        tv_jilu = (TextView)findViewById(R.id.tv_jilu);
        tv_price = (TextView)findViewById(R.id.tv_price);
        tv_no = (TextView)findViewById(R.id.tv_no);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setHasFixedSize(true);

        rv_caozuojilu = (RecyclerView)findViewById(R.id.rv_caozuojilu);
        rv_caozuojilu.setLayoutManager(new LinearLayoutManager(this));
        rv_caozuojilu.setVisibility(View.GONE);

        log_list = orderListBean.getLog_list();

        if(log_list!=null){
            adapter = new OrderDetailAndAdapter(log_list);
        }else{
            log_list = new ArrayList<>();
            adapter = new OrderDetailAndAdapter(log_list);
        }
        recycler.setAdapter(adapter);
    }

    @Override
    public void initTitle() {
        titler.setText("订单详情");
        iv_left.setVisibility(View.VISIBLE);
        iv_left.setText("返回");
        iv_left.setOnClickListener(v->finish());
    }

    @Override
    public void setLayoutView() {
        setContentView(R.layout.activity_order_detail_and);
        orderListBean = (LoginBean.OrderListBean)getIntent().getExtras().getSerializable("data");
    }

    /**
     * 订单记录
     */
    private void orderJilu(){
        progressDialog.setMessage("加载中...");
        progressDialog.show();
        Map<String,String> map = new HashMap<>();
        map.put(ConfigMethod.shop_token, CacheConfig.getInst().getToken(this));
        map.put(ConfigMethod.waiter_token,CacheConfig.getInst().getWaterToken(this));
        map.put(ConfigMethod.order_id,orderId);
        LoggerUtils.loge(orderId);
        RetrofitFactory.getIntance().look_log(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<JiluBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        progressDialog.cancel();
                        LoggerUtils.loge("onError");
                    }

                    @Override
                    public void onNext(JiluBean loginBean) {
                        progressDialog.cancel();
                        LoggerUtils.loge("onNext");
                        if(loginBean.getIs_success().equals("1")){
                            List<JiluBean.ListBean> list = loginBean.getList();
                            if(list == null||list.size() == 0){
                                list = new ArrayList<JiluBean.ListBean>();
                                ToastUtil.showToast(OrderDetailAndActivity.this,"暂无操作记录");
                            }
                            rv_caozuojilu.setAdapter(new CaozuoAdapter(list));
                        }else if(loginBean.getIs_success().equals("0")){
                            ToastUtil.showToast(OrderDetailAndActivity.this,loginBean.getReason());
                        }else if(loginBean.getIs_success().equals("-1")){
                            android.os.Process.killProcess(android.os.Process.myPid());
                        }else if(loginBean.getIs_success().equals("-2")){
                            android.os.Process.killProcess(android.os.Process.myPid());
                        }
                    }
                });
    }
}
